<template>
  <div>
    <!--搜索栏-->
    <el-card id="search">
      <el-row>
        <el-col :span="20">
          <!--左对齐-->
          <el-input v-model="searchModel.jobId" placeholder="Job Id" clearable />
          <el-input v-model="searchModel.limit" placeholder="Limit" clearable />
          <el-button @click="getUploadHistoryList" type="primary" round icon="el-icon-search">查询</el-button>
        </el-col>
        <el-col :span="4" align="right">
          <el-button @click="openEditUI()" type="primary" icon="el-icon-plus" circle />
          <!--右对齐-->
        </el-col>
      </el-row>
    </el-card>

    <!--查询结果-->
    <el-card id="result">
      <el-table :data="uploadHistoryList" stripe style="width: 100%">
        <el-table-column label="#" width="80">
          <template slot-scope="scope">
            {{(searchModel.pageNo-1) * searchModel.pageSize + scope.$index + 1}}
          </template>
        </el-table-column>
        <el-table-column prop="jobId" label="任务ID" width="230">
        </el-table-column>
        <el-table-column prop="table_name" label="数据表名" width="230">
        </el-table-column>
        <el-table-column prop="partition" label="分区数" width="180">
        </el-table-column>
        <el-table-column prop="namespace" label="命名空间" width="180">
        </el-table-column>
        <el-table-column prop="upload_count" label="数据量" width="200">
        </el-table-column>
      </el-table>
    </el-card>
    <!--分页组件-->
    <el-pagination
      @size-change="handleSizeChange"
      @current-change="handleCurrentChange"
      :current-page="searchModel.pageNo"
      :page-sizes="[5, 10, 20, 50]"
      :page-size="searchModel.pageSize"
      layout="total, sizes, prev, pager, next, jumper"
      :total="total">
    </el-pagination>
    <!--提交数据，编辑对话框-->
    <el-dialog @close="clearFileForm" :title="title" :visible.sync="dialogFileFormVisible">
      <el-form :model="dataForm" ref="userFormRef">
        <el-form-item label="数据表名" prop="table_name" placeholder="默认使用文件名" clearable :label-width="formLabelWidth">
          <el-input v-model="dataForm.table_name" autocomplete="off"></el-input>
        </el-form-item>
        <el-form-item label="数据头" prop="head" placeholder="1" clearable :label-width="formLabelWidth">
          <el-input v-model="dataForm.head" autocomplete="off"></el-input>
        </el-form-item>
        <el-form-item label="分区数" prop="partition" placeholder="16" clearable :label-width="formLabelWidth">
          <el-input v-model="dataForm.partition" autocomplete="off"></el-input>
        </el-form-item>
        <el-form-item label="命名空间" prop="namespace" placeholder="experiment" clearable :label-width="formLabelWidth">
          <el-input v-model="dataForm.namespace" autocomplete="off"></el-input>
        </el-form-item>
        <el-form-item label="是否覆盖数据：" :label-width="formLabelWidth">
          <el-switch
            v-model="dataForm.drop"
            active-text="覆盖"
            inactive-text="不覆盖"
          >
          </el-switch>
        </el-form-item>
      </el-form>
      <el-upload
        class="upload-demo"
        action="https://jsonplaceholder.typicode.com/posts/"
        :http-request="getFile">
        <el-button size="small" type="primary">点击上传</el-button>
      </el-upload>
      <div slot="footer" class="dialog-footer">
        <el-button size="small" @click="dialogFileFormVisible = false">取 消</el-button>
        <el-button size="small" type="success" @click="upload">确认上传</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import UpLoadHistoryApi from '@/api/uploadHistoryManage'
export default {
  data() {
    return {
      title: '',
      dialogFileFormVisible: false,
      dataForm: {},
      formLabelWidth: '130px',
      uploadHistoryForm: {},
      total: 0,
      searchModel: {
        jobId: '',
        limit: '',
        pageNo: 1,
        pageSize: 10
      },
      uploadHistoryList: [],
      file: ''
    }
  },

  created() {
    this.getUploadHistoryList()
  },

  methods: {
    clearFileForm() {
      this.file = ''
      this.dataForm = {}
    },
    handleSizeChange(pageSize) {
      this.searchModel.pageSize = pageSize
      this.getUploadHistoryList()
    },
    handleCurrentChange(pageNo) {
      this.searchModel.pageNo = pageNo
      this.getUploadHistoryList()
    },
    openEditUI() {
      this.title = '提交数据'
      this.dialogFileFormVisible = true
    },
    getUploadHistoryList() {
      UpLoadHistoryApi.getUploadHistoryList(this.searchModel).then(response => {
        this.uploadHistoryList = response.data.rows
        this.total = response.data.total
      })
    },
    getFile(item) {
      console.log(item.file)
      this.file = item.file
    },
    upload() {
      UpLoadHistoryApi.uploadFile(this.file, this.dataForm).then(response => {
        // 成功提示
        this.$message({
          message: response.message,
          type: 'success'
        })
        // 关闭对话框
        this.dialogFileFormVisible = false
        // 刷新表格
        this.getUploadHistoryList()
        // 清空数据
        this.dataForm = {}
      })
    }
  }
}
</script>

<style>
#search .el-input {
  width: 150px;
  margin-right: 30px;
}
.el-dialog .el-input {
  width: 85%;
}
</style>
